<template>
  <div class="line-chart">
    <div class="title">{{headTitle}}</div>
    <div :id="chartId"
         :style="{width: '100%', height: '138px'}"></div>
  </div>
</template>

<script>
import * as echarts from "echarts"
export default {
  name: "envCompliance",
  props: {
    chartId: {
      type: String,
      default: "chart"
    },
    headTitle: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    },
    timeData: {
      type: Array,
      required: true
    },
    min: {
      type: String,
      required: true
    },
    max: {
      type: String,
      required: true
    },
    unit: {
      type: String,
      default: ""
    },
    color: {
      type: String,
      default: "#239DD2"
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.chartData,
          this.timeData,
          this.min,
          this.max,
          this.unit
        );
      }
    },
    timeData: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.chartData,
          this.timeData,
          this.min,
          this.max,
          this.unit
        );
      }
    }
  },
  mounted() {
    window.addEventListener("resize", this.resizeHandler);
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeHandler);
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      // console.log(document.getElementById(this.chartId));

      this.chart = echarts.init(document.getElementById(this.chartId));
      this.chart.setOptions(
        this.headTitle,
        this.chartData,
        this.timeData,
        this.min,
        this.max,
        this.unit
      );
    },
    setOptions(title, chartData, timeData, min, max, unit) {
      this.chart.setOption({
        //网格的边据
        grid: {
          left: 20,
          right: 40,
          bottom: 20,
          top: 10,
          containLabel: true
        },
        xAxis: {
          data: timeData,
          //类目轴
          type: 'category',
          //X轴两边对齐
          boundaryGap: false,
          axisTick: {
            //是否显示坐标轴刻度
            show: false,
          }
        },
        yAxis: {
          type: "value",
          name: "单位(" + unit + ")",
          nameLocation: "end",
          min: min,
          max: max,
          boundaryGap: false,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: true
          },
          /*axisLabel: {
            formatter: "{value} " + unit
          }*/
          axisLabel: {
            formatter: function (value) {
              return parseInt(value)
            }
          }
        },
        //提示框组件
        tooltip: {
          //坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用
          trigger: "axis",
          //坐标轴指示器配置项
          axisPointer: {
            type: "cross", //shadow阴影效果
            axis: "y"
          },
          formatter: "{b0}<br />" + title + "：{c0}" + unit,
          padding: [5, 10]
        },
        series: [
          {
            areaStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: this.color // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "rgba(255,255, 255, 0)" // 100% 处的颜色
                  }
                ],
                global: false // 缺省为 false
              }
            },
            itemStyle: {
              normal: {
                color: this.color,
                borderColor: this.color,
                lineStyle: {
                  color: this.color,
                  width: 1
                },
                label: {
                  show: false, //隐藏线点文字
                  color: "#000"
                }
              }
            },
            //name: title,
            smooth: true, // 这个可以显示为平滑过渡的线条
            symbol: "circle", // 这个把节点的空心圆换成实心圆
            symbolSize: 1, // 节点大小，数字随意，跟下面的线宽一样或少 1 像素
            type: "line",
            data: chartData,
            animationDuration: 2800,
            animationEasing: "cubicInOut"
          }
        ]
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.line-chart {
  display: flex;
  .title {
    width: 80px;
    text-align: center;
    font-size: 14px;
    font-weight: 600;
    line-height: 19px;
    color: #0a0a0a;
  }
}
</style>
